const app = getApp()
import trigger from '../../utils/util.js'

Page({
    data: {
        skin_img: '',
        failOrderId: '',
        get_list:'',
        resourceDomain: app.resourceDomain
    },
    onLoad() {
    (trigger.trigger)(this, '../skin/index')
		var thas=this
		app.request(app.apiDomain + 'index.php?c=face&a=prompt',{},(res) => {
			console.log(res)
			thas.setData({
				get_list:res.data.data,
			})
        })
        // 请求广告图
        app.request(app.apiDomain+'index.php?c=ad&a=index',{
            pid: 1
        },res=>{
            console.log(res);
            if(res.data.status==1){
                // console.log(123);
                thas.setData({
                    ad: res.data.data
                })
            }
        })
    },
	ad_click:function(){
		this.setData({
			con_ad_on:'off'
		})
	},
  getUserInfo: function () {
    var that = this
    if (app.getUserInfo(that)) {
      that.setData({
        user_auth: false
      })
    }
  },
	gender_click:function(){
        // 点击判断是否支付过
		if(this.data.get_list.pay){
            // 需要支付 弹窗
			this.setData({
				gender:'on'
			})
		}else{
            // 已支付  选择图片 保存face-id
            // this.addFace()
            this.chooseSkinImage()
            wx.setStorageSync('face_user_id', this.data.get_list.face_user_id)
		}
    },
	gender_off:function(){
		this.setData({
			gender:''
		})
	},
    getFailOrderId(data) {
        let failOrderId = ''
        data.forEach(e => {
            if (e.status == '0') {
                failOrderId = e.trade_no
            }
        })
        this.setData({
            failOrderId: failOrderId
        })
    },
    showConfirm(e) {
		console.log(e.currentTarget.dataset.type)
		this.addFace(e.currentTarget.dataset.type)
    },
    addFace(e) {
        console.log(e);
		var thas=this;
        app.request(app.apiDomain + 'index.php?c=face&a=addfaceUser', {
			face_id:1,
			type:e
		}, (res) => {
            console.log(res)
			thas.setData({
				gender:''
			})
            if (res.data.status == 1) {
// 				thas.face_user_id = res.data.data.face_user_id
// 				wx.setStorageSync('face_user_id', thas.face_user_id)
// 				thas.res = res.data;
// 				if(e=='price' && res.data.data.data.timeStamp){
// 					thas.money_click()
// 				}else{
// 					thas.chooseSkinImage()
// 				}
				thas.chooseSkinImage();
            } else {
				wx.showToast({
					title:res.data.info,
					icon:'none',
					duration: 2000
				})
            }
        })
    },
    chooseSkinImage() {
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success:(res) => {
                this.submitImageUrl(res)
            },
            fail(res) {
                app.request(app.apiDomain + "index.php?c=user&a=uploadFail&type=face&res=" + JSON.stringify(res))
            }
        })
    },
	money_click(){
		var thas=this;
		console.log(thas.face_user_id)
		wx.requestPayment({
			'timeStamp':thas.res.data.data.timeStamp,
			'nonceStr': thas.res.data.data.nonceStr,
			'package': thas.res.data.data.package,
			'signType':thas.res.data.data.signType,
			'paySign':thas.res.data.data.paySign,
			'success':function(res){},
			'fail':function(res){
					wx.showToast({
						title:'支付失败',
						icon: 'none',
						duration: 2000
					})
			},
			'complete':function(res){
					
					if(res.errMsg == "requestPayment:ok"){  // 调用支付成功
						thas.chooseSkinImage()
					}else if(res.errMsg == 'requestPayment:cancel'){
	　　　　　　// 用户取消支付的操作
					wx.showToast({
						title:'已取消支付',
						icon: 'none',
						duration: 2000
					})
		　　　 }
			}
		}) 
	},
    submitImageUrl(res) {
        let that = this
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
            url: app.apiDomain + 'index.php?c=user&a=upload&type=face&openid=' + wx.getStorageSync("openid"),
            filePath: tempFilePaths[0],
            name: 'image',
            formData: {
                type: 'skin'
            },
            success(res) {
                that.goToNextStep(res)
            },
            fail(res) {
                app.request(app.apiDomain + "index.php?c=user&a=uploadFail&type=face&res=" + JSON.stringify(res))
            }
        })
    },
    goToNextStep(res) {
        let data = JSON.parse(res.data)
        this.setData({
            skin_img: app.resourceDomain + data.data
        })
        wx.setStorageSync('skin_img', app.resourceDomain + data.data)
        this.goToCollectPage()
    },
    goToCollectPage() {
        wx.navigateTo({
            url: 'collect'
        })
    },
    checkRecord() {
        wx.navigateTo({
            url: 'history'
        })
    },
    return_act() {
      wx.redirectTo({
        url: '../index/index',
      })
    },
    onReady() {
      app.SystemInfo(this)
    },
    onShareAppMessage() {
        return {
        	path: '/pages/skin/index',
        	title: '我的AI皮肤健康报告出来啦~,你也来试试？'
        }
    }
})
